package com.example.chamberlainserver.Service.impl;

import com.example.chamberlainserver.Entry.Class;
import com.example.chamberlainserver.Service.ClassService;
import com.example.chamberlainserver.dto.ClassDTO;
import com.example.chamberlainserver.dto.PageResult;
import com.example.chamberlainserver.mapper.ClassMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 班级服务实现类
 */
@Service
@Transactional
public class ClassServiceImpl implements ClassService {

    @Autowired
    private ClassMapper classMapper;

    @Override
    public List<Class> getAllClasses() {
        return classMapper.selectAll();
    }

    @Override
    public Class getClassById(Long id) {
        return classMapper.selectById(id.intValue());
    }

    @Override
    public Class getClassByCode(String classCode) {
        return classMapper.selectByClassCode(classCode);
    }

    @Override
    public PageResult<Class> getClassesByPage(int page, int size, String className, String grade, String major, String department, Integer status) {
        // 计算偏移量
        int offset = (page - 1) * size;
        
        // 查询数据
        String statusStr = status != null ? status.toString() : null;
        List<Class> classes = classMapper.selectByPage(offset, size, className, grade, major, department, statusStr);
        
        // 查询总数
        int total = classMapper.countAll(className, grade, major, department, statusStr);
        
        return new PageResult<>(page, size, (long) total, classes);
    }

    // 移除此方法，因为接口中没有定义

    @Override
    public Class createClass(ClassDTO classDTO) {
        // 验证班级代码唯一性
        Class classWithSameCode = classMapper.selectByClassCode(classDTO.getClassCode());
        if (classWithSameCode != null) {
            throw new RuntimeException("班级代码已存在");
        }
        
        // 班级名称验证暂时跳过，因为mapper中没有selectByClassName方法
        
        Class clazz = new Class();
        BeanUtils.copyProperties(classDTO, clazz);
        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());
        
        // 设置默认值
        if (clazz.getStatus() == null) {
            clazz.setStatus("active");
        }
        if (clazz.getStudentCount() == null) {
            clazz.setStudentCount(0);
        }
        
        classMapper.insert(clazz);
        return clazz;
    }

    @Override
    public Class updateClass(Long id, ClassDTO classDTO) {
        Class existingClass = classMapper.selectById(id.intValue());
        if (existingClass == null) {
            throw new RuntimeException("班级不存在");
        }
        
        // 验证班级代码唯一性（排除当前班级）
        Class classWithSameCode = classMapper.selectByClassCode(classDTO.getClassCode());
        if (classWithSameCode != null && !classWithSameCode.getId().equals(id.intValue())) {
            throw new RuntimeException("班级代码已存在");
        }
        
        BeanUtils.copyProperties(classDTO, existingClass, "id", "createTime");
        existingClass.setUpdateTime(LocalDateTime.now());
        
        classMapper.update(existingClass);
        return existingClass;
    }

    @Override
    public boolean deleteClass(Long id) {
        return classMapper.deleteById(id.intValue()) > 0;
    }

    @Override
    public int deleteClasses(List<Long> ids) {
        List<Integer> intIds = new ArrayList<>();
        for (Long id : ids) {
            intIds.add(id.intValue());
        }
        return classMapper.deleteByIds(intIds);
    }

    @Override
    public List<Class> getClassesByCounselorId(Long counselorId) {
        return classMapper.selectByCounselorId(counselorId.intValue());
    }

    @Override
    public List<Class> getUnassignedClasses() {
        return classMapper.selectUnassigned();
    }

    @Override
    public List<Class> getClassesByGrade(String grade) {
        return classMapper.selectByGrade(grade);
    }

    @Override
    public List<Class> getClassesByMajor(String major) {
        return classMapper.selectByMajor(major);
    }

    @Override
    public List<Class> getClassesByGradeAndMajor(String grade, String major) {
        // 暂时返回空列表，因为mapper中没有selectByGradeAndMajor方法
        return new ArrayList<>();
    }

    @Override
    public boolean updateClassStatus(Long id, Integer status) {
        Class clazz = classMapper.selectById(id.intValue());
        if (clazz == null) {
            return false;
        }
        clazz.setStatus(status.toString());
        clazz.setUpdateTime(LocalDateTime.now());
        return classMapper.update(clazz) > 0;
    }

    @Override
    public boolean isClassCodeUnique(String classCode, Long excludeId) {
        Class existingClass = classMapper.selectByClassCode(classCode);
        if (existingClass == null) {
            return true;
        }
        return excludeId != null && existingClass.getId().equals(excludeId.intValue());
    }

    @Override
    public boolean isClassNameUnique(String className, Long excludeId) {
        // 暂时返回true，因为mapper中没有对应方法
        return true;
    }

    @Override
    public List<String> getAllGrades() {
        // 暂时返回空列表，因为mapper中没有selectAllGrades方法
        return new ArrayList<>();
    }

    @Override
    public List<String> getAllMajors() {
        // 暂时返回空列表，因为mapper中没有selectAllMajors方法
        return new ArrayList<>();
    }

    @Override
    public List<String> getAllDepartments() {
        // 暂时返回空列表，因为mapper中没有selectAllDepartments方法
        return new ArrayList<>();
    }
}